﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class VehicleRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<Vehicle> GetAllData()
        {
            return from Vehicle in db.Vehicles
                   orderby Vehicle.VehicleNumber descending
                   select Vehicle;
        }

        public IQueryable<Vehicle> GetAllDataByStatus(string status)
        {
            return from Vehicle in db.Vehicles
                   where Vehicle.VehicleMissionStatusCode == status
                   orderby Vehicle.VehicleNumber descending
                   select Vehicle;
        }

        public IQueryable<Vehicle> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.Vehicles.Where(p => p.VehicleNumber.ToString().Contains(searchString));
            if (searchField == "DoorNo")
                ps = db.Vehicles.Where(p => p.DoorNo.Contains(searchString));
            if (searchField == "LicensePlateNumber")
                ps = db.Vehicles.Where(p => p.LicensePlateNumber.Contains(searchString));
            if (searchField == "CompanyCode")
                ps = db.Vehicles.Where(p => p.CompanyCode.Contains(searchString));
            if (searchField == "VehicleNumber")
                ps = db.Vehicles.Where(p => p.VehicleNumber.Contains(searchString));
            if (searchField == "TrailorNo")
                ps = db.Vehicles.Where(p => p.TrailorNo.Contains(searchString));            

            return ps.AsQueryable();

        }

        public IQueryable<Vehicle> GetDataByStatus(string searchField, string searchOper, string searchString,string status)
        {
            var ps = db.Vehicles.Where(p => p.VehicleNumber.Contains(searchString));
            if (searchField == "DoorNo")
                ps = db.Vehicles.Where(p => p.DoorNo.Contains(searchString));
            if (searchField == "LicensePlateNumber")
                ps = db.Vehicles.Where(p => p.LicensePlateNumber.Contains(searchString));
            if (searchField == "CompanyCode")
                ps = db.Vehicles.Where(p => p.CompanyCode.Contains(searchString));
            if (searchField == "VehicleNumber")
                ps = db.Vehicles.Where(p => p.VehicleNumber.Contains(searchString));
            if (searchField == "TrailorNo")
                ps = db.Vehicles.Where(p => p.TrailorNo.Contains(searchString));

            //return ps.AsQueryable();
            return from Vehicle in ps
                   where Vehicle.VehicleMissionStatusCode == status
                   select Vehicle;


        }
        public IQueryable<Vehicle> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }

        public IQueryable<Vehicle> GetAllDataByStatus(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString, string status)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByStatus(searchField, searchOper, searchString, status).OrderByDescending(sortColumn);
                    else
                        return GetDataByStatus(searchField, searchOper, searchString, status).OrderBy(sortColumn);
                else
                    return GetDataByStatus(searchField, searchOper, searchString, status).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllDataByStatus(status).OrderByDescending(sortColumn);
                    else
                        return GetAllDataByStatus(status).OrderBy(sortColumn);
                else
                    return GetAllDataByStatus(status);
            }
        }



        public Vehicle GetByID(string id)
        {
            return db.Vehicles.SingleOrDefault(Vehicle => Vehicle.VehicleNumber == id);
        }

        //Inset
        public void Add(Vehicle Vehicle)
        {
            //if (Vehicle.StatusID == 0 )
            //    Vehicle.StatusID = 1;
            Vehicle.InsertedUser = HttpContext.Current.User.Identity.Name;
            Vehicle.InsertedDate = DateTime.Now;
            Vehicle.UpdatedUser = HttpContext.Current.User.Identity.Name;
            Vehicle.UpdatedDate = DateTime.Now;
            Vehicle.DataVersion = 1;

            db.Vehicles.AddObject(Vehicle);
        }

        //Update
        public void Update(Vehicle Vehicle)
        {

            Vehicle originalVehicle = GetByID(Vehicle.VehicleNumber);

            Vehicle.VehicleMissionStatusCode = originalVehicle.VehicleMissionStatusCode;
            Vehicle.InsertedUser = originalVehicle.InsertedUser;
            Vehicle.InsertedDate = originalVehicle.InsertedDate;
            Vehicle.UpdatedUser = HttpContext.Current.User.Identity.Name;
            Vehicle.UpdatedDate = DateTime.Now;
            Vehicle.DataVersion = ++originalVehicle.DataVersion;
  
            db.Vehicles.ApplyCurrentValues(Vehicle);
        }

        //Delete
        public void Delete(Vehicle Vehicle)
        {
            db.Vehicles.DeleteObject(Vehicle);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}